/***
 * Copyright (c) 2011 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.neeraj.SharingArtifacts;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class CustomMap extends MapActivity {

	private static final String DEBUG_TAG = "CUSTOM MAP";
	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	Drawable drawable2;
	GeoPoint point0;
	
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay1;
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay3;
	CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay4;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
        mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mapOverlays = mapView.getOverlays();
		Bundle bun = this.getIntent().getExtras();	     
	    String result = bun.getString("json");
	    Double lat = bun.getDouble("latitude");
	    Double lon = bun.getDouble("longitude");
	    
        JSONArray jArray;
		try {			
			itemizedOverlay1 = new CustomItemizedOverlay<CustomOverlayItem>(getResources().getDrawable(R.drawable.marker), mapView);
			itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(getResources().getDrawable(R.drawable.marker2), mapView);
			itemizedOverlay3 = new CustomItemizedOverlay<CustomOverlayItem>(getResources().getDrawable(R.drawable.marker3), mapView);
			itemizedOverlay4 = new CustomItemizedOverlay<CustomOverlayItem>(getResources().getDrawable(R.drawable.marker4), mapView);
        
			if(result.length() > 3) {
				jArray = new JSONArray(result);
				Log.i(DEBUG_TAG, "Json array's length"+jArray.length());
				for(int i=0;i<jArray.length();i++){	              	  
	                JSONObject json_data = jArray.getJSONObject(i);
	//                HashMap<String, String> map = new HashMap<String, String>();
	                if(json_data.getString("artifactTypes").equals("image")){  
	                	Log.i(DEBUG_TAG, "image Iteration"+i);
	            		            		
	            		String fileName = json_data.getString("contentFileName");
	            		point0 = new GeoPoint((int)((Double)json_data.getDouble("latitude")*1E6),(int)((Double)json_data.getDouble("longitude")*1E6));
	            		CustomOverlayItem overlayItem0 = new CustomOverlayItem(point0, fileName, 
	            				"", "");
	            		itemizedOverlay1.addOverlay(overlayItem0);
	                }
	                else if(json_data.getString("artifactTypes").equals("audio")){
	                	Log.i(DEBUG_TAG, "audio Iteration"+i);                	
	            		
	            		String fileName = json_data.getString("contentFileName");
	            		GeoPoint point1 = new GeoPoint((int)((Double)json_data.getDouble("latitude")*1E6),(int)((Double)json_data.getDouble("longitude")*1E6));
	            		CustomOverlayItem overlayItem0 = new CustomOverlayItem(point1, fileName, 
	            				"", "");
	            		itemizedOverlay2.addOverlay(overlayItem0);
	                }
	                else if(json_data.getString("artifactTypes").equals("video")){
	                	Log.i(DEBUG_TAG, "video Iteration"+i);                	
	            		
	            		String fileName = json_data.getString("contentFileName");
	            		GeoPoint point2 = new GeoPoint((int)((Double)json_data.getDouble("latitude")*1E6),(int)((Double)json_data.getDouble("longitude")*1E6));
	            		CustomOverlayItem overlayItem0 = new CustomOverlayItem(point2, fileName, 
	            				"", "");
	            		itemizedOverlay3.addOverlay(overlayItem0);
	                }
	                else if(json_data.getString("artifactTypes").equals("text")){
	                	Log.i(DEBUG_TAG, "text Iteration"+i);                
	            		
	            		String fileName = json_data.getString("contentFileName");
	            		String content = json_data.getString("content");
	            		GeoPoint point3 = new GeoPoint((int)((Double)json_data.getDouble("latitude")*1E6),(int)((Double)json_data.getDouble("longitude")*1E6));
	            		CustomOverlayItem overlayItem0 = new CustomOverlayItem(point3, fileName, 
	            				content, "");
	            		itemizedOverlay4.addOverlay(overlayItem0);	            		
	                }	                
				}
			}
			else {				        		
        		String fileName = "NO Artifacts found";
        		point0 = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
        		CustomOverlayItem overlayItem0 = new CustomOverlayItem(point0, fileName, 
        				"", "");
        		itemizedOverlay1.addOverlay(overlayItem0);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.i(DEBUG_TAG, "Size: "+itemizedOverlay1.size());		
		if(itemizedOverlay1.size()>0){
			mapOverlays.add(itemizedOverlay1);
		}
		if(itemizedOverlay2.size()>0) {
			mapOverlays.add(itemizedOverlay2);
		}
		if(itemizedOverlay3.size()>0) {
			mapOverlays.add(itemizedOverlay3);
		}
		if(itemizedOverlay4.size()>0) {
			mapOverlays.add(itemizedOverlay4);
		}
					
		final MapController mc = mapView.getController();
		mc.animateTo(point0);
		mc.setZoom(16);
		
    }
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}
